<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="600px" @closed="handleClosed">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px" class="demo-ruleForm">
        <el-form-item label="客户端名称" prop="clientName">
          <el-input v-model="form.clientName"></el-input>
        </el-form-item>
        <el-form-item label="描述内容" prop="remark">
          <el-input type="textarea" :rows="3" v-model="form.remark" :maxlength="200" show-word-limit></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { ENGINE_TYPE } from './data'
import { mapActions, mapMutations, mapState } from 'vuex'

export default {
  name: 'addOrEdit',
  props: ['mode', 'data'],
  data() {
    return {
      dialogVisible: true,
      title: this.mode === 1 ? '编辑客户端' : '添加客户端',
      form: {
        clientName: '',
        remark: '',
      },
      rules: {
        clientName: { required: true, message: '请输入' },
      },
      options: ENGINE_TYPE
    }
  },
  created() {
    if (+this.mode) {
      const { clientName, remark } = this.data
      this.form = { clientName, remark }
    }
  },
  methods: {
    ...mapActions({
      setAccessToken: 'SET_ACCESSTOKEN',
      updateAccessToken: 'UPDATE_ACCESSTOKEN',
    }),
    handleConfirm() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.editAccessToken()
          // const params = {
          //   ...this.form,
          //   projectId: this.$route.params.projectId,
          // }
          // if (this.mode) {
          //   // 编辑
          //   this.updateAccessToken(params).then((res) => {
          //     const { code } = res
          //     if (code === 200) {
          //       this.$message.success('操作成功')
          //       this.$emit('success', this.mode)
          //       this.dialogVisible = false
          //     } else {
          //       this.$message.error('操作失败')
          //     }
          //   })
          // } else {
          //   // 新增
          //   this.setAccessToken(params).then((res) => {
          //     const { code } = res
          //     if (code === 200) {
          //       this.$message.success('操作成功')
          //       this.$emit('success', this.mode)
          //       this.dialogVisible = false
          //     } else {
          //       this.$message.error('操作失败')
          //     }
          //   })
          // }
        }
      })
    },
    async editAccessToken() {
      try {
        this.isSubmiting = true
        const params = this.getParams()
        let res = null
        if (this.mode) {
          res = await this.updateAccessToken({ id: this.data.id, params }) // 编辑
        } else {
          res = await this.setAccessToken(params) // 新增
        }
        if (res.code === 200) {
          this.$message.success('操作成功')
          this.$emit('success', this.mode)
          this.dialogVisible = false
        } else {
          this.$message.error('操作失败')
        }
      } catch (e) {
        console.log(e)
      } finally {
        this.isSubmiting = false
      }
    },
    getParams() {
      return {
        ...this.form,
        projectId: this.$route.params.projectId,
      }
    },
    handleClosed() {
      this.$emit('closed')
    }
  }
}
</script>

<style lang="scss" scoped></style>
